package com.mybatis.example.dao;

import com.mybatis.example.dao.utils.IDUtils;
import com.mybatis.example.dao.utils.MyBatisUtils;
import com.mybatis.example.pojo.Blog;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.*;

public class testBlogMapper {
    @Test
    public void testAddBlog(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        mapper.addBlog(new Blog(IDUtils.getId(),"MyBatis basic","Jack",new Date("2013-10-18"),8000));
        mapper.addBlog(new Blog(IDUtils.getId(),"How to learn Java effectively?","Jerry",new Date("2019-02-18"),9999));
        mapper.addBlog(new Blog(IDUtils.getId(),"Basic Spring for rookies.","Jon",new Date("2022-07-18"),5000));
        mapper.addBlog(new Blog(IDUtils.getId(),"Deeply understand Linux!","Eric",new Date("2023-04-01"),6000));
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void testGetBlogs(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

        for (Blog blog : mapper.getBlogs()) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

    @Test
    public void getBlogsIF(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap<String, Object> map = new HashMap<>();
//        map.put("title","Basic Spring for rookies.");
        map.put("author","Jerry");
        for (Blog blog : mapper.getBlogsIF(map)) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

    @Test
    public void testQueryBlogIF( ){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap<String, Object> map = new HashMap<>();
       // map.put("title","Basic Spring for rookies.");
       // map.put("author","Jerry");
        for (Blog blog : mapper.queryBlogIF(map)) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

    @Test
    public void testQueryBlogChoose( ){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap<String, Object> map = new HashMap<>();
         // map.put("title","Basic Spring for rookies.");
         //map.put("author","Jerry");
         //map.put("views",9999);
        for (Blog blog : mapper.queryBlogChoose(map)) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

    @Test
    public void testUpdateBlog( ){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Map<String, Object> map = new HashMap<>();
        map.put("title","Easy to learn Spring.");
        map.put("author","Jacky");
        //map.put("views",9999);
        map.put("id","2dee5294a3cb416eb5492f7101b28e6c");
        mapper.updateBlog(map);
        sqlSession.commit();
        sqlSession.close();
    }


    @Test
    public void testQueryBlogForeach( ){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

        HashMap map = new HashMap();

        ArrayList<String> ids = new ArrayList<>();
        ids.add("2dee5294a3cb416eb5492f7101b28e6c");
        ids.add("7f5e32141b8643ecaa77eedb890661d7");

        map.put("ids",ids);
        List<Blog> blogs = mapper.queryBlogForeach(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

}
